package com.spikything.video {

	import flash.geom.Rectangle;
	import com.spikything.video.*;
	import com.spikything.video.events.VideoEvent;
	
	import flash.display.*;
	import flash.events.*;
	import flash.system.*;
	
	public class VideoControlsMini extends MovieClip {
		
		private var btnFullScreen 			:IconButton;
		private var btnPlay 				:StateButton;
		
		private var disabled 				:Boolean 			= false;
		private var beacon 					:EventDispatcher;
		
		
		public function VideoControlsMini(beacon:EventDispatcher) {
			this.beacon = beacon;
			
			btnPlay = new StateButton("playIcon|pauseIcon");
			btnPlay.addEventListener(MouseEvent.CLICK, playPauseBtnClick);
			addChild(btnPlay);
			btnPlay.x = -10 - btnPlay.width; // = btnPause.x
			btnPlay.y = -btnPlay.height/2; // = btnPause.y
			
			btnFullScreen = new IconButton("fullscreenIcon");
			btnFullScreen.addEventListener(MouseEvent.CLICK, restoreBtnClick);
			addChild(btnFullScreen);
			btnFullScreen.x = 10;
			btnFullScreen.y = -btnPlay.height/2;
			
			/*
			btnPlay = new Sprite();
			btnPlay.addChild(new btnPlayBitmapClass() as Bitmap);
			btnPlay.addEventListener(MouseEvent.CLICK, playPauseBtnClick);
			btnPlay.buttonMode = true;
			btnPlay.mouseChildren = false;
			addChild(btnPlay);
			
			btnPause = new Sprite();
			btnPause.addChild(new btnPauseBitmapClass() as Bitmap);
			btnPause.addEventListener(MouseEvent.CLICK, playPauseBtnClick);
			btnPause.buttonMode = true;
			btnPause.mouseChildren = false;
			addChild(btnPause);
			*/
			
			/*
			btnFullScreen = new Sprite();
			btnFullScreen.addChild(new btnFullScreenBitmapClass() as Bitmap);
			btnFullScreen.addEventListener(MouseEvent.CLICK, restoreBtnClick);
			btnFullScreen.buttonMode = true;
			btnFullScreen.mouseChildren = false;
			addChild(btnFullScreen);
			*/
			
			beacon.addEventListener(VideoEvent.INFO_PLAYING, onVideoPlaying);
			beacon.addEventListener(VideoEvent.INFO_STOPPED, onVideoStopped);
			beacon.addEventListener(VideoEvent.INFO_VIDMOVED, onVideoMoved);
		}
		
		public function onVideoMoved(ve:VideoEvent):void {
			x = ve.locationRect.x + ve.locationRect.width/2;
			y = ve.locationRect.y + ve.locationRect.height/2;
		}
		
		public function playPauseBtnClick(e:Event):void {
			if (disabled) return;
			beacon.dispatchEvent(new VideoEvent(VideoEvent.DO_PLAYPAUSE));
		}
		
		public function restoreBtnClick(e:Event):void {
			if (disabled) return;
			beacon.dispatchEvent(new VideoEvent(VideoEvent.DO_RESTORE));
		}
		
		public function vamoose():void {
			disabled = true;
			Utils.removeChild(parent, this);
		}
		
		public function onVideoPlaying(e:Event=null):void {
			//btnPause.visible = true;
			//btnPlay.visible = false;
			btnPlay.selected = true;
		}
		public function onVideoStopped(e:Event=null):void {
			//btnPause.visible = false;
			//btnPlay.visible = true;
			btnPlay.selected = false;
		}
		
	}
	
}